import { apiClient } from './index'
import { mockApi } from '@/utils/mockApi'
import type { 
  FlinkJob, 
  FlinkJobMapping,
  ApiResponse,
  PaginationResponse,
  QueryParams 
} from '@/types'

export const flinkApi = {
  // 获取作业列表
  getJobs: async (params?: QueryParams): Promise<ApiResponse<PaginationResponse<FlinkJob>>> => {
    return mockApi.flink.getJobs(params)
  },

  // 获取作业详情
  getJobDetail: async (jobId: string): Promise<ApiResponse<FlinkJob>> => {
    const jobs = await mockApi.flink.getJobs()
    const job = jobs.data.items.find(j => j.job_id === jobId)
    return {
      code: 200,
      message: 'success',
      data: job!,
      timestamp: new Date().toISOString()
    }
  },

  // 注册新作业
  registerJob: async (data: Partial<FlinkJob>): Promise<ApiResponse<FlinkJob>> => {
    return {
      code: 200,
      message: 'success',
      data: data as FlinkJob,
      timestamp: new Date().toISOString()
    }
  },

  // 更新作业配置
  updateJob: async (jobId: string, data: Partial<FlinkJob>): Promise<ApiResponse<FlinkJob>> => {
    return {
      code: 200,
      message: 'success',
      data: data as FlinkJob,
      timestamp: new Date().toISOString()
    }
  },

  // 删除作业
  deleteJob: async (jobId: string): Promise<ApiResponse<void>> => {
    return {
      code: 200,
      message: 'success',
      data: undefined,
      timestamp: new Date().toISOString()
    }
  },

  // 停止监控
  stopMonitor: async (jobId: string): Promise<ApiResponse<void>> => {
    return {
      code: 200,
      message: 'success',
      data: undefined,
      timestamp: new Date().toISOString()
    }
  },

  // 恢复监控
  startMonitor: async (jobId: string): Promise<ApiResponse<void>> => {
    return {
      code: 200,
      message: 'success',
      data: undefined,
      timestamp: new Date().toISOString()
    }
  },

  // 启用自动恢复
  enableRecovery: async (jobId: string): Promise<ApiResponse<void>> => {
    return {
      code: 200,
      message: 'success',
      data: undefined,
      timestamp: new Date().toISOString()
    }
  },

  // 禁用自动恢复
  disableRecovery: async (jobId: string): Promise<ApiResponse<void>> => {
    return {
      code: 200,
      message: 'success',
      data: undefined,
      timestamp: new Date().toISOString()
    }
  },

  // 一键重启所有作业
  restartAllJobs: async (config: any): Promise<ApiResponse<any>> => {
    return {
      code: 200,
      message: 'Restart task created',
      data: {
        task_id: 'restart_20240115_103000',
        status: 'PENDING',
        estimated_duration: 300
      },
      timestamp: new Date().toISOString()
    }
  },

  // 重启指定模块
  restartModule: async (config: any): Promise<ApiResponse<any>> => {
    return {
      code: 200,
      message: 'Restart task created',
      data: {
        task_id: 'restart_module_20240115_103000',
        status: 'PENDING',
        estimated_duration: 180
      },
      timestamp: new Date().toISOString()
    }
  },

  // 重启指定作业
  restartJobs: async (config: any): Promise<ApiResponse<any>> => {
    return {
      code: 200,
      message: 'Restart task created',
      data: {
        task_id: 'restart_jobs_20240115_103000',
        status: 'PENDING',
        estimated_duration: 120
      },
      timestamp: new Date().toISOString()
    }
  },

  // 停止所有作业
  stopAllJobs: async (config: any): Promise<ApiResponse<any>> => {
    return {
      code: 200,
      message: 'Stop task created',
      data: {
        task_id: 'stop_all_20240115_103000',
        status: 'PENDING',
        estimated_duration: 60
      },
      timestamp: new Date().toISOString()
    }
  },

  // 停止指定模块
  stopModule: async (config: any): Promise<ApiResponse<any>> => {
    return {
      code: 200,
      message: 'Stop task created',
      data: {
        task_id: 'stop_module_20240115_103000',
        status: 'PENDING',
        estimated_duration: 30
      },
      timestamp: new Date().toISOString()
    }
  },

  // 获取重启任务状态
  getRestartStatus: async (taskId: string): Promise<ApiResponse<any>> => {
    return {
      code: 200,
      message: 'success',
      data: {
        task_id: taskId,
        status: 'RUNNING',
        progress: {
          total_jobs: 18,
          stopped_jobs: 15,
          submitted_jobs: 10,
          successful_jobs: 8,
          failed_jobs: 2
        },
        logs: [
          '2024-01-15 10:30:01 - 开始停止作业...',
          '2024-01-15 10:30:05 - 已停止 15 个作业',
          '2024-01-15 10:30:10 - 开始提交新作业...'
        ],
        estimated_completion: '2024-01-15T10:35:00Z'
      },
      timestamp: new Date().toISOString()
    }
  },

  // 获取停止任务状态
  getStopStatus: async (taskId: string): Promise<ApiResponse<any>> => {
    return {
      code: 200,
      message: 'success',
      data: {
        task_id: taskId,
        status: 'SUCCESS',
        progress: {
          total_jobs: 18,
          stopped_jobs: 18,
          submitted_jobs: 0,
          successful_jobs: 0,
          failed_jobs: 0
        },
        logs: [
          '2024-01-15 10:30:01 - 开始停止作业...',
          '2024-01-15 10:30:05 - 已停止 18 个作业',
          '2024-01-15 10:30:10 - 所有作业已停止'
        ],
        estimated_completion: '2024-01-15T10:30:10Z'
      },
      timestamp: new Date().toISOString()
    }
  },

  // 获取作业统计信息
  getJobStats: async (params?: QueryParams): Promise<ApiResponse<any>> => {
    return {
      code: 200,
      message: 'success',
      data: {
        total_jobs: 18,
        running_jobs: 12,
        stopped_jobs: 3,
        failed_jobs: 2,
        healthy_jobs: 10,
        unhealthy_jobs: 2
      },
      timestamp: new Date().toISOString()
    }
  },

  // 获取作业趋势数据
  getJobTrends: async (params?: QueryParams): Promise<ApiResponse<any>> => {
    return {
      code: 200,
      message: 'success',
      data: {
        trends: [
          { date: '2024-01-09', running: 15, failed: 1 },
          { date: '2024-01-10', running: 16, failed: 0 },
          { date: '2024-01-11', running: 17, failed: 1 },
          { date: '2024-01-12', running: 18, failed: 0 },
          { date: '2024-01-13', running: 17, failed: 1 },
          { date: '2024-01-14', running: 16, failed: 2 },
          { date: '2024-01-15', running: 12, failed: 2 }
        ]
      },
      timestamp: new Date().toISOString()
    }
  },

  // 获取作业映射列表
  getJobMappings: async (params?: QueryParams): Promise<ApiResponse<PaginationResponse<FlinkJobMapping>>> => {
    const mockMappings: FlinkJobMapping[] = [
      {
        id: 1,
        module_name: 'yc-talent-job',
        job_name: 'Talent_Job_Kafka2SR_TDS',
        entry_class: 'com.jcm.TalentJob',
        jar_name: 'yc-talent-job-1.0.0.jar',
        jar_path: '/opt/flink/jars/yc-talent-job-1.0.0.jar',
        default_parallelism: 1,
        default_program_args: '--parallelism 1 --job-name Talent_Job_Kafka2SR_TDS --env test',
        is_active: true,
        description: '抖音号搜索得到百应UID数据',
        created_at: '2024-01-01T00:00:00Z',
        updated_at: new Date().toISOString()
      }
    ]
    
    return {
      code: 200,
      message: 'success',
      data: {
        items: mockMappings,
        pagination: {
          page: 1,
          size: 20,
          total: mockMappings.length,
          pages: 1
        }
      },
      timestamp: new Date().toISOString()
    }
  },

  // 获取作业映射详情
  getJobMappingDetail: async (mappingId: number): Promise<ApiResponse<FlinkJobMapping>> => {
    return {
      code: 200,
      message: 'success',
      data: {} as FlinkJobMapping,
      timestamp: new Date().toISOString()
    }
  },

  // 创建作业映射
  createJobMapping: async (data: Partial<FlinkJobMapping>): Promise<ApiResponse<FlinkJobMapping>> => {
    return {
      code: 200,
      message: 'success',
      data: data as FlinkJobMapping,
      timestamp: new Date().toISOString()
    }
  },

  // 更新作业映射
  updateJobMapping: async (mappingId: number, data: Partial<FlinkJobMapping>): Promise<ApiResponse<FlinkJobMapping>> => {
    return {
      code: 200,
      message: 'success',
      data: data as FlinkJobMapping,
      timestamp: new Date().toISOString()
    }
  },

  // 删除作业映射
  deleteJobMapping: async (mappingId: number): Promise<ApiResponse<void>> => {
    return {
      code: 200,
      message: 'success',
      data: undefined,
      timestamp: new Date().toISOString()
    }
  },

  // 启用/禁用作业映射
  toggleJobMapping: async (mappingId: number, isActive: boolean): Promise<ApiResponse<void>> => {
    return {
      code: 200,
      message: 'success',
      data: undefined,
      timestamp: new Date().toISOString()
    }
  },

  // 批量导入映射
  batchImportMappings: async (data: { mappings: Partial<FlinkJobMapping>[] }): Promise<ApiResponse<any>> => {
    return {
      code: 200,
      message: 'success',
      data: { imported_count: data.mappings.length },
      timestamp: new Date().toISOString()
    }
  },

  // 验证映射配置
  validateMapping: async (data: Partial<FlinkJobMapping>): Promise<ApiResponse<any>> => {
    return {
      code: 200,
      message: 'success',
      data: { valid: true },
      timestamp: new Date().toISOString()
    }
  },

  // 批量导入作业
  batchImportJobs: async (data: { jobs: Partial<FlinkJob>[] }): Promise<ApiResponse<any>> => {
    return {
      code: 200,
      message: 'success',
      data: { imported_count: data.jobs.length },
      timestamp: new Date().toISOString()
    }
  },

  // 启用所有作业自动恢复
  enableAllRecovery: async (): Promise<ApiResponse<void>> => {
    return {
      code: 200,
      message: 'success',
      data: undefined,
      timestamp: new Date().toISOString()
    }
  },

  // 禁用所有作业自动恢复
  disableAllRecovery: async (): Promise<ApiResponse<void>> => {
    return {
      code: 200,
      message: 'success',
      data: undefined,
      timestamp: new Date().toISOString()
    }
  },
} 